<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="上次登录时间" prop="lastLoginDate">
      <el-input v-model="dataForm.lastLoginDate" placeholder="上次登录时间"></el-input>
    </el-form-item>
    <el-form-item label="上次扫描时间" prop="lastScanDate">
      <el-input v-model="dataForm.lastScanDate" placeholder="上次扫描时间"></el-input>
    </el-form-item>
    <el-form-item label="微信id" prop="wxId">
      <el-input v-model="dataForm.wxId" placeholder="微信id"></el-input>
    </el-form-item>
    <el-form-item label="卡密" prop="cdkey">
      <el-input v-model="dataForm.cdkey" placeholder="卡密"></el-input>
    </el-form-item>
    <el-form-item label="昵称" prop="nickname">
      <el-input v-model="dataForm.nickname" placeholder="昵称"></el-input>
    </el-form-item>
    <el-form-item label="登录ip" prop="ip">
      <el-input v-model="dataForm.ip" placeholder="登录ip"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    <el-form-item label="创建人" prop="createdBy">
      <el-input v-model="dataForm.createdBy" placeholder="创建人"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createdDate">
      <el-input v-model="dataForm.createdDate" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="最后更新人" prop="lastUpdatedBy">
      <el-input v-model="dataForm.lastUpdatedBy" placeholder="最后更新人"></el-input>
    </el-form-item>
    <el-form-item label="最后更新时间" prop="lastUpdatedDate">
      <el-input v-model="dataForm.lastUpdatedDate" placeholder="最后更新时间"></el-input>
    </el-form-item>
    <el-form-item label="头像" prop="headimg">
      <el-input v-model="dataForm.headimg" placeholder="头像"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          lastLoginDate: '',
          lastScanDate: '',
          wxId: '',
          cdkey: '',
          nickname: '',
          ip: '',
          remark: '',
          createdBy: '',
          createdDate: '',
          lastUpdatedBy: '',
          lastUpdatedDate: '',
          headimg: ''
        },
        dataRule: {
          lastLoginDate: [
            { required: true, message: '上次登录时间不能为空', trigger: 'blur' }
          ],
          lastScanDate: [
            { required: true, message: '上次扫描时间不能为空', trigger: 'blur' }
          ],
          wxId: [
            { required: true, message: '微信id不能为空', trigger: 'blur' }
          ],
          cdkey: [
            { required: true, message: '卡密不能为空', trigger: 'blur' }
          ],
          nickname: [
            { required: true, message: '昵称不能为空', trigger: 'blur' }
          ],
          ip: [
            { required: true, message: '登录ip不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ],
          createdBy: [
            { required: true, message: '创建人不能为空', trigger: 'blur' }
          ],
          createdDate: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          lastUpdatedBy: [
            { required: true, message: '最后更新人不能为空', trigger: 'blur' }
          ],
          lastUpdatedDate: [
            { required: true, message: '最后更新时间不能为空', trigger: 'blur' }
          ],
          headimg: [
            { required: true, message: '头像不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/cards/amgcdkusers/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.lastLoginDate = data.amgcdkusers.lastLoginDate
                this.dataForm.lastScanDate = data.amgcdkusers.lastScanDate
                this.dataForm.wxId = data.amgcdkusers.wxId
                this.dataForm.cdkey = data.amgcdkusers.cdkey
                this.dataForm.nickname = data.amgcdkusers.nickname
                this.dataForm.ip = data.amgcdkusers.ip
                this.dataForm.remark = data.amgcdkusers.remark
                this.dataForm.createdBy = data.amgcdkusers.createdBy
                this.dataForm.createdDate = data.amgcdkusers.createdDate
                this.dataForm.lastUpdatedBy = data.amgcdkusers.lastUpdatedBy
                this.dataForm.lastUpdatedDate = data.amgcdkusers.lastUpdatedDate
                this.dataForm.headimg = data.amgcdkusers.headimg
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/cards/amgcdkusers/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'lastLoginDate': this.dataForm.lastLoginDate,
                'lastScanDate': this.dataForm.lastScanDate,
                'wxId': this.dataForm.wxId,
                'cdkey': this.dataForm.cdkey,
                'nickname': this.dataForm.nickname,
                'ip': this.dataForm.ip,
                'remark': this.dataForm.remark,
                'createdBy': this.dataForm.createdBy,
                'createdDate': this.dataForm.createdDate,
                'lastUpdatedBy': this.dataForm.lastUpdatedBy,
                'lastUpdatedDate': this.dataForm.lastUpdatedDate,
                'headimg': this.dataForm.headimg
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
